package cn.com.shopec.erp.product.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.product.dao.IProductDao;
import cn.com.shopec.erp.product.entity.Product;
import cn.com.shopec.erp.product.vo.ProductVo1;
import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionProductVo;

/**
 * 
 * @ClassName: ProductDaoImpl1
 * @Description: 货品数据访问层DAO实现类
 * @author: duhui
 * @date 2013-5-29 下午03:53:57
 * 
 */
@Repository
public class ProductDaoImpl extends HibernateEntityDao<Product> implements IProductDao {

	/**
	 * 根据供应商编号获取所对应的货品
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ProductVo1> queryPurchaseList(ProductVo1 productVo1, Integer pageCount, Integer pageSize) {
		List<ProductVo1> purchase = null;
		if(productVo1!=null){
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT "
					+ "t.ID as id,"
					+ "t.PROD_NO as prodNo,"
					+ "t.COMMO_NO as commoNo,"
					+ "t.PROD_BAR_CODE as prodBarCode,"
					+ "t.INVENTORY_TYPE as inventoryType,"
					+ "t.SALE_TYPE as saleType,"
					+ "t.SALE_PRICE as salePrice,"
					+ "t.SPECIFICATION as specification,"
					+ "t.DELETE_FLAG as deleteFlag,"
					+ "t.CASE_PACK_SIZE as casePakgSize,"
					+ "t.CREATE_TIME as createTime,"
					+ "t.UPDATE_TIME as updateTime,"
					+ "t.UN_SALE_PRICE as unSalePrice,"
					+ "t.PROD_RATE as prodRate,"
					+ "t.BUY_NUM as buyNum,"
					+ "t.PROD_UNIT as prodUnit,"
					+ "r.IS_RETURNED_GOODS as isReturnedGoos"
					+ " from TBL_PRODUCT t , TBL_PROD_SUPPLIER r ,TBL_COMMODITY y "
					+ "where t.PROD_NO = r.PROD_NO and t.commo_no = y.commo_no "
					+ "and t.DELETE_FLAG = 1 and r.SUPPLIER_NO = ? and y.check_state= 1");
					//+ "and t.DELETE_FLAG = 1 and r.SUPPLIER_NO = \'"+productVo1.getCode()+"\' ");
			Query query= getSession().createSQLQuery(sql.toString())
					.addScalar("id",StandardBasicTypes.STRING)
					.addScalar("prodNo",StandardBasicTypes.STRING)
					.addScalar("commoNo",StandardBasicTypes.STRING)
					.addScalar("prodBarCode",StandardBasicTypes.STRING)
					.addScalar("inventoryType",StandardBasicTypes.INTEGER)
					.addScalar("saleType",StandardBasicTypes.INTEGER)
					.addScalar("salePrice",StandardBasicTypes.DOUBLE)
					.addScalar("specification",StandardBasicTypes.STRING)
					.addScalar("deleteFlag",StandardBasicTypes.INTEGER)
					.addScalar("casePakgSize",StandardBasicTypes.STRING)
					.addScalar("createTime",StandardBasicTypes.STRING)
					.addScalar("updateTime",StandardBasicTypes.STRING)
					.addScalar("unSalePrice",StandardBasicTypes.DOUBLE)
					.addScalar("prodRate",StandardBasicTypes.DOUBLE)
					.addScalar("buyNum",StandardBasicTypes.INTEGER)
					.addScalar("prodUnit",StandardBasicTypes.STRING)
					.addScalar("isReturnedGoos",StandardBasicTypes.STRING);
			if (pageCount != null && pageSize != null) {
				query.setFirstResult(pageCount).setMaxResults(pageSize);
			}
			query.setParameter(0, productVo1.getCode());
			query.setResultTransformer(Transformers.aliasToBean(ProductVo1.class));
//		    purchase = (PurchaseRequisitionProduct) query.uniqueResult();
			purchase = (List<ProductVo1>) query.list();
		}
		return purchase;
	}
}
